package com.example.springbootdatamock.mock;

import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Component
@RestController
public class RedisMock
        implements Runnable {
    private static final Logger log = LoggerFactory.getLogger(RedisMock.class);


    @Value("${sleepMs.redisMockSleep}")
    private Integer redisMockSleep;

    @Value("${dest.redisDataKey}")
    private String redisDataKey;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/redisMock")
    public void run() {
        String[] brands = {"ZTE", "小米", "VIVO", "OPPO", "华为", "IPHONE", "荣耀", "REALME", "IQQO", "ONEPLUS"};


        while (true) {
            this.redisTemplate.opsForZSet().incrementScore(this.redisDataKey, brands[

                    RandomUtils.nextInt(0, brands.length)], 1.0D);

            log.info("redis开始写数据");
            try {
                Thread.sleep(this.redisMockSleep.intValue());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public void print() {
        System.out.println(this.redisDataKey);
        System.out.println(this.redisTemplate);
        System.out.println(this.redisMockSleep);
    }
}



 